package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.ChoiceFormat;
import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Format extends JFrame {

	private JPanel contentPane;
	private JButton btnDecimal;
	private JButton btnDecimal_1;
	private JButton btnSimple;
	private JButton btnChoice;
	private JButton btnMessage;
	private JTextArea textArea_1;
	private JTextArea textArea;
	private JTextArea res;
	private JTextArea textArea_2;
	private JTextArea textArea_3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Format frame = new Format();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Format() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 558, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getBtnDecimal());
		contentPane.add(getButton_1());
		contentPane.add(getButton_1_1());
		contentPane.add(getBtnChoice());
		contentPane.add(getBtnMessage());
		contentPane.add(getTextArea_1());
		contentPane.add(getTextArea());
		contentPane.add(getRes());
		contentPane.add(getTextArea_2());
		contentPane.add(getTextArea_3());
	}
	
	private JButton getBtnDecimal() {
		if (btnDecimal == null) {
			btnDecimal = new JButton("StringFormat");
			btnDecimal.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String str1="";
					boolean flag=false;
					if(!textArea.getText().equals("")) flag=true;
					if(!textArea_1.getText().equals("")) flag=true;
					if(flag){
						if(textArea.getText().charAt(0)>45&&textArea.getText().charAt(0)<97){
						try{
							int d=Integer.parseInt(textArea.getText());
							String st1=String.format("입금 : %d원",d);
							String st2=String.format("입금 :%10d원",d);
							String st3=String.format("입금 :%-10d원",d);
							res.append(st1);
							res.append(st2);
							res.append(st3);
						}catch(Exception e){
							float f=Float.parseFloat(textArea.getText());
							String st2=String.format("입금 :%10.1f, 이자: %10.3f",f,f);
							res.append(st2);
						}
						}else{

							String st=String.format("성명: %s --->학점 :%s",textArea.getText(),textArea_1.getText());
						
							res.append(st);
							
						}
					}
				}
			});
			btnDecimal.setBounds(0, 229, 92, 23);
		}
		return btnDecimal;
	}
	private JButton getButton_1() {
		if (btnDecimal_1 == null) {
			btnDecimal_1 = new JButton("Decimal");
			btnDecimal_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					DecimalFormat df=new DecimalFormat("#,###,###.##");
					String str=df.format(Float.parseFloat(textArea.getText()));
					
					res.append(str);
				}
			});
			btnDecimal_1.setBounds(99, 229, 92, 23);
		}
		return btnDecimal_1;
	}
	private JButton getButton_1_1() {
		if (btnSimple == null) {
			btnSimple = new JButton("Simple");
			btnSimple.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					SimpleDateFormat sd=new SimpleDateFormat("yyyy년 MM월 dd일 (E요일)");
					SimpleDateFormat sd2=new SimpleDateFormat("hh:mm:ss a");
					
					String str1=sd.format(new Date());
					String str2=sd.format(new Date());
					
					res.append(str1);
					res.append("\n");
					res.append(str2);
				}
			});
			btnSimple.setBounds(201, 229, 92, 23);
		}
		return btnSimple;
	}
	private JButton getBtnChoice() {
		if (btnChoice == null) {
			btnChoice = new JButton("Choice");
			btnChoice.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0){
					double[] limit={0,60,70,80,90};
					String pattern ="0<F|60<D|70<C|80<B|90<A";
					String grade[]={"F","D","C","B","A"};
					int score[]={Integer.parseInt(textArea.getText()),Integer.parseInt(textArea_1.getText())
							,Integer.parseInt(textArea_2.getText()),Integer.parseInt(textArea_3.getText())};
					
					ChoiceFormat cf=new ChoiceFormat(limit,grade);
					ChoiceFormat cf2=new ChoiceFormat(pattern);
					for(int i:score){
						String str=String.format("%4d-->%s",i,cf.format(i));
						res.append(str);
					}
					res.append("\n");
					for(int i:score){
						String str=String.format("%4d-->%s",i,cf2.format(i));
						res.append(str);
					}
					//res.append(arg0);
					
				}
			});
			btnChoice.setBounds(305, 229, 92, 23);
		}
		return btnChoice;
	}
	private JButton getBtnMessage() {
		if (btnMessage == null) {
			btnMessage = new JButton("Message");
			btnMessage.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String str="성명 :{0},생년월일 :{1},주소:{2}";
					String data[][]={{textArea.getText(),textArea_1.getText(),textArea_2.getText()}};
					
					for(String s[]:data){
						String r=MessageFormat.format(str, s);
						res.append(r);
					}
				}
			});
			btnMessage.setBounds(409, 229, 92, 23);
		}
		return btnMessage;
	}
	private JTextArea getTextArea_1() {
		if (textArea_1 == null) {
			textArea_1 = new JTextArea();
			textArea_1.setBounds(111, 181, 80, 23);
		}
		return textArea_1;
	}
	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
			textArea.setBounds(12, 181, 80, 23);
		}
		return textArea;
	}
	private JTextArea getRes() {
		if (res == null) {
			res = new JTextArea();
			res.setBounds(12, 10, 518, 135);
		}
		return res;
	}
	private JTextArea getTextArea_2() {
		if (textArea_2 == null) {
			textArea_2 = new JTextArea();
			textArea_2.setBounds(213, 181, 80, 23);
		}
		return textArea_2;
	}
	private JTextArea getTextArea_3() {
		if (textArea_3 == null) {
			textArea_3 = new JTextArea();
			textArea_3.setBounds(317, 181, 80, 23);
		}
		return textArea_3;
	}
}
